<?php
namespace app\admin\controller;
use app\admin\model\Page2 as Page2Model;

class Page2 extends Base
{
    public function lst()
    {
        $itemRes = Page2Model::order('id ASC')->paginate(10);
        $this->assign('itemRes', $itemRes);
        return view();
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');

            $validate = \think\Loader::validate('Other');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $other = new OtherModel();

            if($other->save($data)){
                $this->success('添加内页信息成功',url('lst'));
            }else{
                $this->error('添加内页信息失败！');
            }
            return;
        }
        $pnames = array(
            ['id'=>1,'name'=>'项目案例'],
            ['id'=>2,'name'=>'企业动态'],
            ['id'=>3,'name'=>'加入我们'],
            ['id'=>4,'name'=>'联系我们'],
        );
        $this->assign('pnames',$pnames);
        return view();
    }

    public function edit(){
        $id = input('id');
        $other = new OtherModel();
        if (request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Other');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
                die;
            }
            $data = [
                'id'        =>input('id'),
                'title'     =>input('title'),
                'keywords'  =>input('keywords'),
                'desc'      =>input('desc'),
            ];
            $save = $other->update($data);
            if($save){
                $this->success('修改内页信息成功','lst');
            }else{
                $this->error('修改内页信息失败');
            }
            return;
        }
        $others = $other->find($id);

        $pnames = array(
            ['id'=>1,'name'=>'项目案例'],
            ['id'=>2,'name'=>'企业动态'],
            ['id'=>3,'name'=>'加入我们'],
            ['id'=>4,'name'=>'联系我们'],
        );
        $this->assign(array(
            'pnames'     =>$pnames,
            'others'     =>$others
        ));
        return view();
    }

    public function del(){
        $id = input('id');
        if(OtherModel::destroy($id)){
            $this->success("删除内页信息成功！", 'lst');
        }else{
            $this->error('删除内页信息失败');
        }
    }
}
